package Jogo;

import java.awt.Color;
import java.util.*;


public class ControlGame {

	private Vector<Pieces> pieces_vector, scoring_pieces_vector;
	private int chosenPiece=0;
	private Pieces p;
	private Color color1 = Color.blue;
	private Color color2 = Color.red;	
	private Board board;
	//private int scoring1,scoring2;
	
	private int turn = 1;
	private boolean endGame = false;
	private boolean endMove = false;
	private int balance=0; //if balance>0, team 1 tilted team 2.
	private int winner=0;
	
	//create pieces - initial board state (tested)
	public ControlGame(){
		p = new Pieces();
		board=new Board();
		
		int lpieces=6;
		int lweight=6;
		int lcolumn1= 1; //team1
		int lcolumn2= 12;	//team2
		int lstartline= 3;
		
		int mpieces=10;
		int mweight=4;
		int mcolumn1= 2;
		int mcolumn2= 11;
		int mstartline= 2;
		
		int spieces=14;
		int sweight=2;
		int scolumn1= 3;
		int scolumn2= 10;
		int sstartline= 1;
		
		int scpieces=18;
		int scweight=4;
		int sccolumn1= 0;
		int sccolumn2= 13;
		int scstartline= 0;
		Color sccolor = Color.white;
		
		int team1=1, team2=2;
		
		int totalPieces;
		
		totalPieces = lpieces+mpieces+spieces+scpieces;

		pieces_vector = new Vector<Pieces>(totalPieces);
		//scoring_pieces_vector = new Vector<Pieces>(scpieces);
		
		//add pieces
		for (int i=0;i<totalPieces;i++){
			
			if (i<lpieces/2){
				pieces_vector.add(new Pieces(color1, lweight, lcolumn1, lstartline+i, "Large", team1));
			}else if(i<lpieces){
				pieces_vector.add(new Pieces(color2, lweight, lcolumn2, lstartline+i-(lpieces/2), "Large", team2));
			}else if(i<lpieces+mpieces/2){
				pieces_vector.add(new Pieces(color1, mweight, mcolumn1, mstartline+i-(lpieces), "Medium", team1));
			}else if(i<lpieces+mpieces){
				pieces_vector.add(new Pieces(color2, mweight, mcolumn2, mstartline+i-(lpieces+mpieces/2), "Medium", team2));
			}else if(i<lpieces+mpieces+spieces/2){
				pieces_vector.add(new Pieces(color1, sweight, scolumn1, sstartline+i-(lpieces+mpieces), "Small", team1));
			}else if(i<lpieces+mpieces+spieces){
				pieces_vector.add(new Pieces(color2, sweight, scolumn2, sstartline+i-(lpieces+mpieces+spieces/2), "Small", team2));
			}else if(i<lpieces+mpieces+spieces+scpieces/2){
				pieces_vector.add(new Pieces(sccolor, scweight, sccolumn1, scstartline+i-(lpieces+mpieces+spieces), "Scoring", team1));
				//scoring_pieces_vector.add(new Pieces(sccolor, scweight, sccolumn1, scstartline+i-(lpieces+mpieces+spieces), "Scoring", team1));
			}else if(i<lpieces+mpieces+spieces+scpieces){
				pieces_vector.add(new Pieces(sccolor, scweight, sccolumn2, scstartline+i-(lpieces+mpieces+spieces+scpieces/2), "Scoring", team2));
				//scoring_pieces_vector.add(new Pieces(sccolor, scweight, sccolumn1, scstartline+i-(lpieces+mpieces+spieces), "Scoring", team1));
			}
		 }
	
	}
	
	public Pieces getScoringPieces(int i){
		return scoring_pieces_vector.get(i);
	}
		
	//
	public Pieces getPieceByIndex(int i){
		 return pieces_vector.get(i);
	}
	
	//
	public Pieces getPieceByXY (int x, int y){
		
		if(isTherePiece(x, y)){
			return pieces_vector.get(chosenPiece);
		}
		else{ //temp?
			System.out.println("There's no piece here (" + x+","+ y + ") to get. Try again.");
			return pieces_vector.get(0); 
		}
	}
	
	//move piece (from start point to end point
	public void movePiece(int fromX, int fromY, int toX, int toY){
		
		if(isTherePiece(fromX, fromY)){
			pieces_vector.get(chosenPiece).setSpotX(toX);
			pieces_vector.get(chosenPiece).setSpotY(toY);
			board.setSpotBoard(fromX, fromY, toX, toY);
		}
		//temporary?
		else {
			System.out.println("There's no piece here (" + fromX +","+ fromY + ") to move. Try again.");
		}
			

	}
	
	
	//find which piece (vector index) is selected	
	//o que acontece se eu der x,y que nao tem ninguem?
	public boolean isTherePiece(int x, int y){
		for (int i = 0; i < pieces_vector.size(); i++) {
			if(pieces_vector.get(i).getSpotX()==x && pieces_vector.get(i).getSpotY()==y){
				chosenPiece=i;
				return true; //there's a piece here!
			}
		}
		chosenPiece=50;
		return false; //there isnt a piece here..
	}
	
	//
	public int getChosenPiece(int x, int y){
		if(isTherePiece(x, y)){
			return chosenPiece;
		}
		else {
			System.out.println("There's no piece here (" + x+","+ y + "). Try again.");
			return chosenPiece = 50;
		}
	}

	//check if board is balanced. return true is so.
	public boolean isBalanced(){
		int balanceT1=0, balanceT2=0, balance=0 ;
		int CONSTBALANCE = 30;
		//T1 = lado esquerdo, T2 = lado direito.
		for (int i = 0; i < pieces_vector.size(); i++) {
			if(pieces_vector.get(i).getSpotX()<7){
				// balance = X * weight
				balanceT1+=(pieces_vector.get(i).getSpotX())*(pieces_vector.get(i).getWeight());
			}
			else{
				balanceT2+=(pieces_vector.get(i).getSpotX())*(pieces_vector.get(i).getWeight());
			}
		}
		
		
		balance=Math.abs(balanceT1-balanceT2);
		this.balance=balanceT2-balanceT1; //momento t2 > momento t1
		if(balance<CONSTBALANCE){		
			return true;
		}
		else {
			return false;
		}
		
	}
	
	public int getScoring(int team){
		int j=0;
		for (int i = 0; i < scoring_pieces_vector.size(); i++) {
			if(pieces_vector.get(i).getName()=="Scoring" && pieces_vector.get(i).getTeam()==team){
			//if(scoring_pieces_vector.get(i).getTeam()==team){
				j++;
			}
		}			
		return j;
	}

	public int getVectorPiecesSize(){
		return pieces_vector.size();
	}
	
	public void switchTurn(){	
		if(turn==1){
			turn=2;
		}else{turn=1;}
	}

	public void removeScoring(int team){
		
		for (int i = 0; i < pieces_vector.size(); i++) {
			p=getPieceByIndex(i);
			if(p.getTeam()==team && p.getName()=="Scoring"){
					pieces_vector.remove(i);
					break;
			}		
		}
			
	}
		
	public int getTurn(){
		return turn;
	}
	public void setTurn(int turn){
		this.turn=turn;
	}
	
	public boolean isEndMove(){
		return endMove;
	}
	
	public void setEndMove(boolean endMove){
		this.endMove=endMove;
	}
	
	private boolean isEndGame(){
		//check if all pieces are in opponent's Scoring Zone and your sc> opponent_Sc
		int scoreT1=1, scoreT2=1;
		
		//check balance. while is not balanced, remove scoring pieces.
		scoreT1=getScoring(1);
		scoreT2=getScoring(2);
		while(!isBalanced() && scoreT1>0 && scoreT2>0){
			if(balance>0){ //tombou pro lado de T2 (direito da tela)=> T2 tira scoring
				removeScoring(2);
			}
			else{
				removeScoring(1);
			}
			scoreT1=getScoring(1);
			scoreT2=getScoring(2);
		}
		if (isBalanced()){
			endGame=false;
		}
		else if(scoreT1==0 || scoreT2==0){
			if(balance>0){
				winner=1;
				endGame=true;
			}
			else{
				winner=2;
				endGame=true;
			}
		}

		
		
		return endGame;
	}
	
	public boolean getEndGame(){
		return endGame;
	}
	
	public void setWinner(int team){
		winner=team;
	}
	public int getWinner(){
		return winner;
	}
	
    public void jogo(int xfrom, int yfrom, int xto, int yto){
		movePiece(xfrom, yfrom, xto, yto);
		setEndMove(true);
//		if(isEndGame()){
//			//handle End Game
//		}
//		else{
			switchTurn();
//		}
		
}

	public boolean canMoveTo(int x, int y){
		//code method..
		if(isTherePiece(x, y) || x==0 || x ==13){ //cant move to where theres a piece or to scoring spot
			return false;
		}
		else return true;
	}
}
